﻿namespace PMS.Client.FrmFile
{
    using PMS.BLL;
    using PMS.Client.DataService;
    using PMS.Common;
    using PMS.Model;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.IO;
    using System.Windows.Forms;

    public class FrmAdd : Form
    {
        private FileBLL bll = new FileBLL();
        private Button btnCancel;
        private Button btnSave;
        private Button btnScan;
        private IContainer components = null;
        private Label label1;
        private Label lbInfo;
        private TextBox txtPath;

        public FrmAdd()
        {
            this.InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.lbInfo.Visible = true;
            byte[] binaryFile = FileConverter.GetBinaryFile(this.txtPath.Text);
            string fileName = this.txtPath.Text.Substring(this.txtPath.Text.LastIndexOf('\\') + 1);
            DataServiceClient client = new DataServiceClient();
            bool flag = false;
            try
            {
                flag = client.UploadFile(fileName, binaryFile);
                this.lbInfo.Text = "上传成功";
            }
            catch (Exception exception)
            {
                Log.Write(exception.Message);
                flag = false;
            }
            if (flag)
            {
                PMS.Model.File file2 = new PMS.Model.File {
                    AddDate = DateTime.Now,
                    AddUser = PMS.Common.Keys.Admin.LoginName,
                    FileName = fileName,
                    FileMD5 = DESEncrypt.GetMd5(Convert.ToBase64String(binaryFile)),
                    FileSize = binaryFile.Length,
                    FilePath = @"\" + Config.FilePath + @"\" + fileName,
                    UpdateDate = DateTime.Now,
                    UpdateUser = PMS.Common.Keys.Admin.LoginName
                };
                PMS.Model.File model = file2;
                if (this.bll.Add(model) > 0)
                {
                    System.IO.File.Copy(this.txtPath.Text, Application.StartupPath + model.FilePath);
                    SyslogBLL.Write("上传文件", "上传文件 " + fileName + " 成功");
                    base.DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("添加失败！");
                }
            }
            else
            {
                MessageBox.Show("上传失败！");
            }
        }

        private void btnScan_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog {
                Filter = "Word文档(*.doc)|*.doc|Word2007文档(*.docx)|*.docx"
            };
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                this.txtPath.Text = dialog.FileName;
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(FrmAdd));
            this.label1 = new Label();
            this.txtPath = new TextBox();
            this.btnCancel = new Button();
            this.btnSave = new Button();
            this.btnScan = new Button();
            this.lbInfo = new Label();
            base.SuspendLayout();
            this.label1.AutoSize = true;
            this.label1.Location = new Point(0x16, 0x29);
            this.label1.Name = "label1";
            this.label1.Size = new Size(0x35, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "选择文件";
            this.txtPath.Anchor = AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            this.txtPath.BackColor = Color.FromArgb(0xd9, 0xea, 0xf7);
            this.txtPath.BorderStyle = BorderStyle.FixedSingle;
            this.txtPath.Location = new Point(0x51, 0x27);
            this.txtPath.Name = "txtPath";
            this.txtPath.Size = new Size(390, 0x15);
            this.txtPath.TabIndex = 2;
            this.btnCancel.BackColor = Color.White;
            this.btnCancel.FlatStyle = FlatStyle.Popup;
            this.btnCancel.Location = new Point(0x150, 0x70);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new Size(0x4b, 0x17);
            this.btnCancel.TabIndex = 6;
            this.btnCancel.Text = "取消";
            this.btnCancel.UseVisualStyleBackColor = false;
            this.btnCancel.Click += new EventHandler(this.btnCancel_Click);
            this.btnSave.BackColor = Color.White;
            this.btnSave.FlatStyle = FlatStyle.Popup;
            this.btnSave.Location = new Point(150, 0x70);
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new Size(0x4b, 0x17);
            this.btnSave.TabIndex = 5;
            this.btnSave.Text = "保存";
            this.btnSave.UseVisualStyleBackColor = false;
            this.btnSave.Click += new EventHandler(this.btnSave_Click);
            this.btnScan.BackColor = Color.White;
            this.btnScan.FlatStyle = FlatStyle.Popup;
            this.btnScan.Location = new Point(0x1dd, 0x27);
            this.btnScan.Name = "btnScan";
            this.btnScan.Size = new Size(0x3b, 0x15);
            this.btnScan.TabIndex = 7;
            this.btnScan.Text = "浏览";
            this.btnScan.UseVisualStyleBackColor = false;
            this.btnScan.Click += new EventHandler(this.btnScan_Click);
            this.lbInfo.AutoSize = true;
            this.lbInfo.Location = new Point(0x4f, 0x4b);
            this.lbInfo.Name = "lbInfo";
            this.lbInfo.Size = new Size(0xad, 12);
            this.lbInfo.TabIndex = 8;
            this.lbInfo.Text = "文件上传中，请耐心等待......";
            this.lbInfo.Visible = false;
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = AutoScaleMode.Font;
            this.BackColor = Color.FromArgb(0xa4, 0xc4, 0xeb);
            base.ClientSize = new Size(560, 0x93);
            base.Controls.Add(this.lbInfo);
            base.Controls.Add(this.btnScan);
            base.Controls.Add(this.btnCancel);
            base.Controls.Add(this.btnSave);
            base.Controls.Add(this.txtPath);
            base.Controls.Add(this.label1);
            base.FormBorderStyle = FormBorderStyle.FixedSingle;
            base.Icon = (Icon) manager.GetObject("$this.Icon");
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "FrmAdd";
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "添加文件";
            base.ResumeLayout(false);
            base.PerformLayout();
        }
    }
}

